<?php
if(!class_exists('XMapping', false)){
	require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'XMapping.php');
}
class XManyToMany extends XMapping{
	protected $relation = null;
	
	function setRelation($relation){
		$this->relation = $relation;
	}		
	
	function relation(){
		return $this->relation;
	}
	
	function result($value = null){
		$relations = array();
		$relations = q($this->relation())
			->eq($this->column(), $value)
			->findAll();
		if(!is_array($relations) || count($relations) == 0){
			return array();
		}
		$ids = x_array_pick($relations, $this->foreignKey());
		$query = q($this->target())->uniqueId($ids);
		foreach($this->orders() as $field => $order){
			$query = $query->$order($field);
		}
		return $query->findAll();
	}
}
?>